﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HanGu.Services.Interface;
using HanGu.Entity;

namespace HanGu.Services
{
    public class LibraryService : BaseService,ILibrary
    {

        public List<Library> GetAll()
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Library.OrderByDescending(c =>c.UpdateTime).ToList();
            }
        }

        public List<Library> GetAll(UniversallySearchParam searchparma)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                var query = obj.Library.Where(c=>1==1);
                if (!string.IsNullOrEmpty(searchparma.Ttile))
                {
                    query = query.Where(c => c.Title.IndexOf(searchparma.Ttile)>=0);
                }
                if (searchparma.StarDateTime.HasValue)
                {
                    query = query.Where(c => c.UpdateTime >= searchparma.StarDateTime);
                }
                if (searchparma.EndDateTime.HasValue)
                {
                    query = query.Where(c=>c.UpdateTime<=searchparma.EndDateTime);
                }
                query = base.Paging<Library,DateTime?>(c=>c.UpdateTime, query,searchparma.Pagination);

                return query.ToList();
            }
        }

        public Library GetByID(int id)
        {
            using (HaiGuEntities obj = new HaiGuEntities())
            {
                return obj.Library.Where(c => c.ID == id).FirstOrDefault();
            }
        }

       
        public bool Add(Library model)
        {
            try
            {
                this.Add<Library>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Update(Library model)
        {
            try
            {
                this.Update<Library>(model);
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int id)
        {
            try
            {
                this.Delete<Library>(c => c.ID == id);
                return true;
            }
            catch { return false; }
        }

        
    }
}
